package utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public final class FabricaSessaoHibernate {
	
	private static FabricaSessaoHibernate fabrica;
	private static SessionFactory sessionFactory;
	
	private FabricaSessaoHibernate(){
	
	}
	
	public static FabricaSessaoHibernate ObterInstancia(){
		if(fabrica == null){
			fabrica = new FabricaSessaoHibernate();
		}		
		return fabrica;
	}
	
	public  SessionFactory getSessionFactory() {
		return recuperarFabrica();
	}

	private  SessionFactory recuperarFabrica(){		
		if(sessionFactory == null){
			AnnotationConfiguration acfg = new AnnotationConfiguration();
			acfg.configure();
//			SchemaExport schemaExport = new SchemaExport(acfg);
//			schemaExport.create(true, true);
			sessionFactory = acfg.buildSessionFactory();
		}
		
		return sessionFactory;		
	}
	
	public Session recuperarSessao(){
		return getSessionFactory().openSession();
	}
	
	public void fecharFabrica(){
		sessionFactory.close();
	}
}